package b3.scene
{
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	
	import flash.display.Sprite;
	import flash.events.*;

	public class SceneManager extends Sprite
	{
		static private var instance:SceneManager;
		public var currentScene:BasicScene;
		public var previousScene:BasicScene;
		//场景层
		public var sceneLayer:Sprite;
		//首层（最上层）
		public var topLayer:Sprite;
		 
		public function SceneManager()
		{
			super();
			sceneLayer = new Sprite();
			topLayer = new Sprite();
			addChild(sceneLayer);
			addChild(topLayer);
			ServerHelper.getInstance().addEventListener(ServerEvent.OTHER_LOGIN, onOtherLogin);
		}
		
		private function onOtherLogin(event:ServerEvent):void
		{
			var messageBox:MessageBox = new MessageBox("用户已经在其他地方登陆！\n请重新登陆！");
			messageBox.setOKCall(ServerRequest.redirectToGamePage);
			WidgetManager.getInstance().popOut([messageBox], true);
		}

		static public function getInstance():SceneManager
		{
			if (!instance)
				instance = new SceneManager();
			return instance;
		}
		
		public function loadScene(scene:BasicScene):void
		{
			if (!scene)
				return;
			if (currentScene != scene)
			{
				unloadScene();
				sceneLayer.addChild(scene);
				currentScene = scene;
			} 
		}
		
		public function unloadScene():void
		{
			if (currentScene)
			{
				sceneLayer.removeChild(currentScene);
				previousScene = currentScene;
			}
		}
		
		public function loadPreviousScene():void
		{
			loadScene(previousScene);
		}
		
	}
}